﻿using System;
using System.Collections.Generic;
using System.Linq;
using RayDen.Library.Components.SystemComponents;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data.Imaging;
using RayDen.RayEngine.Core.Interface;

namespace RayDen.RayEngine
{
    public class LightEmissionProfilesManager
    {

        private static readonly LightEmissionProfilesManager instance = new LightEmissionProfilesManager();

        public static LightEmissionProfilesManager Instance
        {
            get { return instance; }
        }

        private EmissionProfile[] profiles;
        private Dictionary<string, EmissionProfile> dic;

        public EmissionProfile this[string name]
        {
            get
            {
                if (!dic.ContainsKey(name))
                    return dic["default"];
                return dic[name.ToLowerInvariant()];
            }
        }

        protected LightEmissionProfilesManager()
        {

            profiles = new[]
                {
                    new EmissionProfile
                        {
                            Name = "default",
                            EmissionSpectra = new RgbSpectrum(0.75f).ToArray(),
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering 

                        }, 
                    new EmissionProfile()
                        {
                            Name = "d6500",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.A, SpectrumType.Illuminant).ToArray()
                        }, 
                          new EmissionProfile()
                        {
                            Name = "A",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.A, SpectrumType.Illuminant).ToArray()
                        }, 
                    new EmissionProfile()
                        {
                            Name = "candle",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.FromFile(@"F:\3D\spds\Lights\candle.spd"), SpectrumType.Illuminant).ToArray(),
                        }, 
                        new EmissionProfile()
                        {
                            Name = "daylight",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.FromFile(@"F:\3D\spds\Lights\daylight.spd"), SpectrumType.Illuminant).ToArray(),
                        }, 
                     new EmissionProfile()
                        {
                            Name = "cfl6k",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.FromFile(@"F:\3D\spds\Lights\cfl6k.spd"), SpectrumType.Illuminant).ToArray(),
                        }, 

                     new EmissionProfile()
                        {
                            Name = "inc1",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.FromFile(@"F:\3D\spds\Lights\Incandescent1.spd"), SpectrumType.Illuminant).ToArray(),
                        }, 
                          new EmissionProfile()
                        {
                            Name = "d5000",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionSpectra = SPD_Data.FromSampled(SPD_Data.A, SpectrumType.Illuminant).ToArray()
                        }, 
                    new EmissionProfile()
                        {
                            Name = "smooth",
                            InvertTexture = true,
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionTexture = ImageFactory.FreeImageLoadBitmap(@"F:\3D\Emission\particle1.bmp")
                        }, 
                    new EmissionProfile()
                        {
                            Name = "swirl",
                            SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                            EmissionTexture = ImageFactory.FreeImageLoadBitmap(@"F:\3D\Emission\elecswirl.jpg")
                        }, 
                    //new EmissionProfile()
                    //    {
                    //        Name = "caustic",
                    //        SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                    //        EmissionTexture = ImageFactory.FreeImageLoadBitmap(@"F:\3D\Emission\caustic.gif")
                    //    }, 
                    //new EmissionProfile()
                    //    {
                    //        Name = "scatter",
                    //        SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                    //        EmissionTexture = ImageFactory.FreeImageLoadBitmap(@"F:\3D\Emission\scatr4.gif")
                    //    }, 
                    //    new EmissionProfile()
                    //    {
                    //        Name = "noise",
                    //        SpectralRendering = GlobalConfiguration.Instance.SpectralRendering,
                    //        EmissionTexture = ImageFactory.FreeImageLoadBitmap(@"F:\3D\Textures\Noise\gray_flash.png")
                    //    }, 
                };

            dic = profiles.ToDictionary(i => i.Name, StringComparer.InvariantCultureIgnoreCase);
        }


        public void AddProfile(EmissionProfile profile)
        {
            this.dic.Add(profile.Name.ToLowerInvariant(), profile);
        }
    }
}